#include <math.h>
#include <stdio.h>
#include <stdlib.h>
//#include <device.h>

#include "fdecl.h"
//#include "display.h"
#include <string.h>

#include <qapplication.h>
#include "qtWin_SQ.h"


hybroid *Hybroids[MAX_NUM_HYBROIDS];
hybroid *h1;

int Num_Hybroids = 0, Num_Constraints = 0;

/* Define lightsource and material Properties */

//float
//  material1[]  = { AMBIENT,  0.9, 0.7, 0.7, /*0.9, 0.7, 0.7,*/
//		     DIFFUSE,  0.7, 0.9, 0.3,
//		     SPECULAR, 0.8, 0.2, 0.1,
//		     SHININESS, 100.0, LMNULL },
//
//  light[]     = { POSITION, -10.0, -100.0, 100.0, 0.0, LMNULL },
//  lmodel[]    = { LMNULL };

void read_Hybroids(const char *filename)
{
	hybroid *h;
	int nhybr = 0;	/* num of hybr - 1*/
	FILE *infile;

  for(int i = 0; i <= nhybr;i++) { 
    printf("Reading file: %s\n", filename);
    if((infile = fopen(filename, "r")) == 0) {
      printf("Opening error on file: %s\n", filename) ;
      return ;
     }
    h = make_hybroid (infile); 
    allocate_sin_cos_arrays(h->usize,h->vsize); 
    compute_sin_cos_arrays(h->usize, h->vsize);
    assemble_hybroid_rectangular_mesh(h);
    compute_element_stiffness_matrix(h); 
    compute_superquadric_taper_bend(h);
    Hybroids[Num_Hybroids++] = h;
  }
}


void tmain(int argc, char *argv[]) 
{ 
	int i, j, k, l, m, count = 0, fnum = 3; 
	int nconst = 0/*number of constr - 1*/;
	int frame_n =0, Window;
	vector initial_axis;
	char base_name[256], dump_filename[256], junk[256];
	hybroid *h;
	

  strcpy(base_name,"file");
  if (argc < 2) 
    { printf("Usage: suq filename\n"); exit(0); } 


  /* now set the scene for the SGI */

  //foreground(); 
  //prefposition(4, 500, 4, 100);
  //Window = winopen("Superquadric"); 
  //winconstraints(); 
  //RGBmode(); 
  //doublebuffer();
  //user_init(); 
  //gconfig(); 
  //mmode(MVIEWING); 
  //set_projection(); 
  //clear(); 

	read_Hybroids(argv[1]);

  h1 = Hybroids[0];
  Hybroids[0]->translation.x = 0.0;
  
  initial_axis.x = 0.0; initial_axis.y = 1.0; initial_axis.z = 0.0;
  unit_quaternion(0.0, initial_axis, Hybroids[0]->orientation);
  unit_quaternion_to_rotation_matrix(Hybroids[0]->orientation, 
                                     Hybroids[0]->rotation);

 
  read_ascii_xyz_data_hybroids(argv[2], Hybroids, Num_Hybroids, 105.0);  

  while (1) { //(!getbutton(ESCKEY)) {

	  // change parameters
    //user();

    for(i = 0; i < Num_Hybroids; i++) zeroize_force(Hybroids[i]);

    if (count == 360) count=0; count++;

    //RGBcolor(0, 0, 30);
    //clear();

    //RGBcolor(255, 255, 255);

    for(i = 0; i < Num_Hybroids; i++) {
      h = Hybroids[i];

      if(i != 2)
      compute_range_data_forces_exhaustive(h);

    }

    for(i = 0; i < Num_Hybroids; i++) {
        if(i != 2)
        compute_motion(Hybroids[i]);    
        h = Hybroids[i];
	if(i == 2){
         compute_positions(h);
         unrotate_force(h);

	 compute_rigid_motion(h);
	 compute_superquadric_deformation_taper_bend(h);

       }

	// draw triangular mesh

     //   RGBcolor(0, 255, 255);
     //   draw_triangular_mesh(h, posit);
       
/*
        draw_elements_flat_shading(h);
*/
      }

    //RGBcolor(255, 0, 0);
/* SOS need to change this function not to display the relevant data i =2 */   
    //display_range_data_hybroids(Hybroids, Num_Hybroids);

    /* Push this button when you need to bring a new frame */
    if(0){	//(getbutton(F1KEY) == 1){
     read_ascii_xyz_data_hybroids(argv[fnum], Hybroids, Num_Hybroids, 115.0);  
      fnum++;
    }

    //swapbuffers();
  /* routine to store the program output */

	if (0){ //(getbutton(F2KEY)==1) {
	sprintf(dump_filename, "%s-%d.rgb", base_name, frame_n);
	dump_window(Window, dump_filename);
	printf("DUMP_WINDOW: Enter filename: ");
	scanf("%s", junk);
	frame_n++;
	}    
  }
}

int main(int argc, char** argv)
{
	QApplication app(argc, argv);
	qtWin_SQ widget;


	widget.show();
	return app.exec();
}

void dump_window(int wid, char *filename)
{
  char command_string[256];
  long x, y, xo, yo;

  // set current window as active, and get its size and origin
  //winset(wid);
  //getsize(&x, &y);
  //getorigin(&xo, &yo);
  sprintf(command_string, "scrsave %s %d %d %d %d",
          filename, xo, xo + x - 1, yo, yo + y - 1);

  system(command_string);
}




void calc_normal (vector *p1, vector *p2, vector *p3, vector *norm)
{

   float coa, cob, coc ;
    float px1, py1, pz1 ;
    float px2, py2, pz2 ;
    float px3, py3, pz3 ;

    float absvec ;

    px1 = p1->x ;
    py1 = p1->y ;
    pz1 = p1->z ;

    px2 = p2->x ;
    py2 = p2->y ;
    pz2 = p2->z ;

    px3 = p3->x ;
    py3 = p3->y ;
    pz3 = p3->z ;

    coa = -(py1 * (pz2-pz3) + py2*(pz3-pz1) + py3*(pz1-pz2)) ;
    cob = -(pz1 * (px2-px3) + pz2*(px3-px1) + pz3*(px1-px2)) ;
    coc = -(px1 * (py2-py3) + px2*(py3-py1) + px3*(py1-py2)) ;

    absvec = sqrtf ((double) ((coa*coa) + (cob*cob) + (coc*coc))) ;

    norm->x = coa/absvec ;
    norm->y = cob/absvec ;
    norm->z = coc/absvec ;

 }